package cn.mylife.common.domain;

import cn.mylife.common.enums.RespCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

/**
 * 响应消息
 *
 * @author: 潘海江
 * @date: 2020-06-11 18:55:51
 */
@Setter
@Getter
public class Resp {

    public static Resp success() {
        return new Resp(RespCode.SUCCESS, RespCode.SUCCESS.msg);
    }

    public static Resp saveSuccess() {
        return new Resp(RespCode.SUCCESS, "保存成功");
    }

    public static Resp addSuccess() {
        return new Resp(RespCode.SUCCESS, "新增成功");
    }

    public static Resp modifySuccess() {
        return new Resp(RespCode.SUCCESS, "修改成功");
    }

    public static Resp removeSuccess() {
        return new Resp(RespCode.SUCCESS, "删除成功");
    }

    public static Resp success(String msg) {
        return new Resp(RespCode.SUCCESS, msg);
    }

    public static Resp success(Object data) {
        if (data instanceof String) {
            return new Resp(RespCode.SUCCESS, data.toString());
        }
        return new Resp(RespCode.SUCCESS, "SUCCESS", data);
    }

    public static Resp success(String msg, Object data) {
        return new Resp(RespCode.SUCCESS, msg, data);
    }

    public static Resp fail() {
        return new Resp(RespCode.FAIL, RespCode.FAIL.msg);
    }

    public static Resp saveFail() {
        return new Resp(RespCode.FAIL, "保存失败");
    }

    public static Resp addFail() {
        return new Resp(RespCode.FAIL, "新增失败");
    }

    public static Resp modifyFail() {
        return new Resp(RespCode.FAIL, "修改失败");
    }

    public static Resp removeFail() {
        return new Resp(RespCode.FAIL, "删除失败");
    }

    public static Resp fail(String msg) {
        return new Resp(RespCode.FAIL, msg);
    }

    public static Resp fail(int code, String msg) {
        return new Resp(code, msg, null);
    }

    public static Resp fail(RespCode code) {
        return new Resp(code, code.msg);
    }

    public static Resp fail(RespCode code, String message) {
        return new Resp(code, message);
    }

    private int code;

    private String msg;

    private Object data;

    public Resp() {
    }

    public Resp(RespCode code, String msg) {
        this.code = code.code;
        this.msg = msg;
    }

    public Resp(RespCode code, String msg, Object data) {
        this.code = code.code;
        this.msg = msg;
        this.data = data;
    }

    public Resp(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.code == RespCode.SUCCESS.code;
    }

    @JsonIgnore
    public boolean isFail() {
        return this.code != RespCode.SUCCESS.code;
    }

    public <T> T data() {
        if (data == null) return null;
        return (T) data;
    }

    public void toSuccess() {
        this.toSuccess("SUCCESS");
    }

    public void toSuccess(String msg) {
        this.code = RespCode.SUCCESS.code;
        this.msg = msg;
    }

    public void toFail(String msg) {
        this.toFail(RespCode.FAIL, msg);
    }

    public void toFail(RespCode code, String msg) {
        this.code = code.code;
        this.msg = msg;
    }

}
